# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 工具创建-结构化工具.py
# @Author: dongguangwen
# @Date  : 2025-03-23 21:25
import time
import asyncio
from langchain_core.tools import StructuredTool


def multiply(a: int, b: int) -> int:
    """Multiply two numbers."""
    time.sleep(3)
    return a * b


async def amultiply(a: int, b: int) -> int:
    """Multiply two numbers."""
    time.sleep(1)
    return a * b


calculator = StructuredTool.from_function(func=multiply, coroutine=amultiply)


async def main1():
    print(calculator.invoke({"a": 2, "b": 3}))
    print(await calculator.ainvoke({"a": 2, "b": 5}))


async def main2():
    print(await calculator.ainvoke({"a": 22, "b": 5}))
    print(calculator.invoke({"a": 22, "b": 3}))


asyncio.run(main1())  # 运行异步任务
asyncio.run(main2())  # 运行异步任务

"""
6
10
110
66
"""